//
//  RHListViewModel.swift
//  微博新浪
//
//  Created by hezerong on 16/6/18.
//  Copyright © 2016年 Rocky. All rights reserved.
//

import UIKit

class RHListViewModel: NSObject {
    
    lazy var statusViewModelArray:[RHStatusViewModel] = []


    func loadMoreDataNew(isPullUp:Bool , finish:(success:Bool)->()){
        
        print("下拉刷新")

        
        var since_id = statusViewModelArray.first?.statusModel.id ?? 0
        var max_id = statusViewModelArray.last?.statusModel.id ?? 0
        
        if isPullUp{
            //传max_id ,since_id设为0，获取的数据放到数据源的后面
            since_id = 0
            
        }else{
            //传since_id, max_id设为0， 获取的数据放到数据源的前面
            max_id = 0
        }
        
        //测试显示单条信息代码  
//        since_id = 0
//        max_id =   3988008271526650
        
        RHNetWorkTool.sharedTool.loadStatus(since_id, max_id: max_id) { (response) in
            
            
            if let response = response , statues = response["statuses"] as? [[String: AnyObject]]{
                
                //临时数组
                var responseViewModelArray:[RHStatusViewModel] = []
                
                for dict in statues{
                    
                    //字典转模型
                    let status = RHStatusModel.yy_modelWithJSON(dict)
                    print(status)
                    let statusViewModel = RHStatusViewModel(statusModel:status!)
                    //添加到临时数组
                    responseViewModelArray.append(statusViewModel)
                    
                }
                
                if isPullUp{
                    
                    //把临时数组添加到数据数组中
                    responseViewModelArray.removeAll()
                    
                    self.statusViewModelArray += responseViewModelArray
                }else{
                    self.statusViewModelArray = responseViewModelArray + self.statusViewModelArray
                }
                
               
                finish(success: true)
            }
            
            
        }
        
        
    }
}
